package com.zhixin.zhixinmall.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONArray;
import com.zhixin.zhixinmall.entity.pojo.JdElite;
import com.zhixin.zhixinmall.entity.pojo.JdGroup;
import com.zhixin.zhixinmall.entity.pojo.JdSwpier;
import com.zhixin.zhixinmall.entity.result.ResultMap;
import com.zhixin.zhixinmall.entity.vo.JdEliteGoodVo;
import com.zhixin.zhixinmall.entity.vo.JdEliteGoodsVo;
import com.zhixin.zhixinmall.entity.vo.JdEliteVo;
import com.zhixin.zhixinmall.entity.vo.JdSwiperGoodVo;
import com.zhixin.zhixinmall.service.DtkService;
import com.zhixin.zhixinmall.service.JdEliteService;
import com.zhixin.zhixinmall.service.JdGroupService;
import com.zhixin.zhixinmall.service.JdService;
import com.zhixin.zhixinmall.service.JdSwiperService;
import com.zhixin.zhixinmall.util.SnowflakeIdWorker;

/**
 * @author cyc
 * @date 2023/10/09 10:05:50
 */

@RestController
@CrossOrigin
@RequestMapping("/jd")
public class JdController {
        private Logger logger = LoggerFactory.getLogger(JdController.class);

        @Autowired
        private JdGroupService jdGroupService;

        @Autowired
        private JdSwiperService jdSwiperService;

        @Autowired
        private JdService jdService;

        @Autowired
        private JdEliteService jdEliteService;

        @Autowired
        private DtkService dtkService;

        private SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 1);

        @GetMapping("/index/swiper")
        @Cacheable(value = "indexSwiper", keyGenerator = "keyGenerator", unless = "#result == null ")
        public ResultMap indexSwiper() {
                List<JdSwpier> list = jdSwiperService.list();
                Map<String, Object> map = new HashMap<>(1);
                map.put("result", list);
                return ResultMap.ok(map);
        }

        @GetMapping("/index/swiper/goods/{eliteId}")
        @Cacheable(value = "indexSwiperGoods", keyGenerator = "keyGenerator", unless = "#result == null ")
        public ResultMap indexSwiperGoods(@PathVariable("eliteId") Integer eliteId,
                        @RequestParam("total") Integer total) {
                JSONArray executeJingfenQuery = jdService.executeJingfenQuery(eliteId, null, null,
                                "commissionShare",
                                "desc");
                List<JdSwiperGoodVo> jdSwiperGoodVos = new ArrayList<>();

                for (int i = 0; i < executeJingfenQuery.size(); i++) {
                        JdSwiperGoodVo jdSwiperGoodVo = new JdSwiperGoodVo();
                        jdSwiperGoodVo.setSkuid(Long.parseLong(executeJingfenQuery.getJSONObject(i).get("skuId")
                                        .toString()));
                        jdSwiperGoodVo
                                        .setImgUrl(executeJingfenQuery.getJSONObject(i).getJSONObject("imageInfo")
                                                        .getJSONArray("imageList")
                                                        .getJSONObject(0).getString("url"));
                        jdSwiperGoodVos.add(jdSwiperGoodVo);
                }
                jdSwiperGoodVos = jdSwiperGoodVos.subList(0, total);
                Map<String, Object> map = new HashMap<>(1);
                map.put("result", jdSwiperGoodVos);
                return ResultMap.ok(map);
        }

        @GetMapping("/group/list")
        @Cacheable(value = "groupList", keyGenerator = "keyGenerator", unless = "#result == null ")
        public ResultMap groupList() {
                List<JdGroup> list = jdGroupService.list();
                Map<String, Object> map = new HashMap<>(1);
                map.put("result", list);
                return ResultMap.ok(map);
        }

        @GetMapping("/elite/list")
        @Cacheable(value = "eliteList", keyGenerator = "keyGenerator", unless = "#result == null ")
        public ResultMap eliteList(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                        @RequestParam(value = "pageSize", defaultValue = "2") Integer pageSize,
                        @RequestParam(value = "sortName", defaultValue = "commissionShare") String sortName,
                        @RequestParam(value = "sort", defaultValue = "desc") String sort) {
                List<JdElite> list = jdEliteService.list();
                List<JdEliteGoodsVo> jdEliteGoodsVos = new ArrayList<>();
                list.forEach(jdElite -> {
                        JSONArray executeJingfenQuery = jdService.executeJingfenQuery(jdElite.getEliteId(), pageIndex,
                                        pageSize,
                                        sortName, sort);
                        JdEliteGoodsVo jdEliteGoodsVo = new JdEliteGoodsVo();
                        BeanUtils.copyProperties(jdElite, jdEliteGoodsVo);
                        List<JdEliteGoodVo> goods = new ArrayList<>();

                        for (int i = 0; i < executeJingfenQuery.size(); i++) {
                                JdEliteGoodVo jdEliteGoodVo = new JdEliteGoodVo();
                                jdEliteGoodVo.setSkuId(Long.parseLong(
                                                executeJingfenQuery.getJSONObject(i).get("skuId")
                                                                .toString()));
                                jdEliteGoodVo.setImgUrl(executeJingfenQuery.getJSONObject(i)
                                                .getJSONObject("imageInfo")
                                                .getJSONArray("imageList")
                                                .getJSONObject(0).getString("url"));
                                jdEliteGoodVo.setPrice(executeJingfenQuery.getJSONObject(i)
                                                .getJSONObject("priceInfo")
                                                .getString("price"));
                                goods.add(jdEliteGoodVo);
                        }
                        jdEliteGoodsVo.setGoodsList(goods);
                        jdEliteGoodsVos.add(jdEliteGoodsVo);
                });
                Map<String, Object> map = new HashMap<>(1);
                map.put("result", jdEliteGoodsVos);
                return ResultMap.ok(map);
        }

        @GetMapping("/group/{groupId}")
        @Cacheable(value = "group", keyGenerator = "keyGenerator", unless = "#result == null")
        public ResultMap groupGoods(@PathVariable("groupId") String groupId,
                        @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                        @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                        @RequestParam(value = "sortName", defaultValue = "commissionShare") String sortName,
                        @RequestParam(value = "sort", defaultValue = "desc") String sort) {
                JdGroup group = jdGroupService.getGroupByGroupId(Long.parseLong(groupId));
                JSONArray executeJingfenGroupQuery = jdService.executeJingfenGroupQuery(Long.parseLong(groupId),
                                Long.parseLong(group.getOwnerUnionId()), pageIndex, pageSize, sortName, sort);
                if (executeJingfenGroupQuery == null) {
                        return ResultMap.error("获取失败");
                }
                List<JdEliteGoodVo> jdEliteVos = new ArrayList<>();

                for (int i = 0; i < executeJingfenGroupQuery.size(); i++) {
                        JdEliteGoodVo jdEliteGoodVo = new JdEliteGoodVo();
                        jdEliteGoodVo
                                        .setSkuId(Long.parseLong(executeJingfenGroupQuery.getJSONObject(i).get("skuId")
                                                        .toString()));
                        jdEliteGoodVo.setSkuName(executeJingfenGroupQuery.getJSONObject(i).get("skuName").toString());
                        jdEliteGoodVo.setMaterialUrl(
                                        executeJingfenGroupQuery.getJSONObject(i).get("materialUrl").toString());
                        jdEliteGoodVo.setPromotionUrl(dtkService.promotionUnionConvert(jdEliteGoodVo.getSkuId(),
                                        jdEliteGoodVo.getMaterialUrl()));
                        jdEliteGoodVo
                                        .setImgUrl(executeJingfenGroupQuery.getJSONObject(i).getJSONObject("imageInfo")
                                                        .getJSONArray("imageList")
                                                        .getJSONObject(0).getString("url"));
                        jdEliteGoodVo
                                        .setPrice(executeJingfenGroupQuery.getJSONObject(i).getJSONObject("priceInfo")
                                                        .getString("price"));
                        jdEliteGoodVo.setComments(Integer
                                        .parseInt(executeJingfenGroupQuery.getJSONObject(i).getString("comments")));
                        jdEliteVos.add(jdEliteGoodVo);
                }
                Map<String, Object> map = new HashMap<>(1);
                map.put("result", jdEliteVos);
                return ResultMap.ok(map);
        }

        @GetMapping("/elite/{eliteId}")
        @Cacheable(value = "elite", keyGenerator = "keyGenerator", unless = "#result == null")
        public ResultMap eliteGoods(@PathVariable("eliteId") Integer eliteId,
                        @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                        @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                        @RequestParam(value = "sortName", defaultValue = "commissionShare") String sortName,
                        @RequestParam(value = "sort", defaultValue = "desc") String sort) {
                JSONArray executeJingfenGroupQuery = jdService.executeJingfenQuery(eliteId, pageIndex, pageSize,
                                sortName, sort);
                if (executeJingfenGroupQuery == null) {
                        return ResultMap.error("获取失败");
                }
                List<JdEliteGoodVo> jdEliteVos = new ArrayList<>();

                for (int i = 0; i < executeJingfenGroupQuery.size(); i++) {
                        JdEliteGoodVo jdEliteGoodVo = new JdEliteGoodVo();
                        jdEliteGoodVo
                                        .setSkuId(Long.parseLong(executeJingfenGroupQuery.getJSONObject(i).get("skuId")
                                                        .toString()));
                        jdEliteGoodVo.setSkuName(executeJingfenGroupQuery.getJSONObject(i).get("skuName").toString());
                        jdEliteGoodVo.setMaterialUrl(
                                        executeJingfenGroupQuery.getJSONObject(i).get("materialUrl").toString());
                        jdEliteGoodVo.setPromotionUrl(dtkService.promotionUnionConvert(jdEliteGoodVo.getSkuId(),
                                        jdEliteGoodVo.getMaterialUrl()));
                        jdEliteGoodVo
                                        .setImgUrl(executeJingfenGroupQuery.getJSONObject(i).getJSONObject("imageInfo")
                                                        .getJSONArray("imageList")
                                                        .getJSONObject(0).getString("url"));
                        jdEliteGoodVo
                                        .setPrice(executeJingfenGroupQuery.getJSONObject(i).getJSONObject("priceInfo")
                                                        .getString("price"));
                        jdEliteGoodVo.setComments(Integer
                                        .parseInt(executeJingfenGroupQuery.getJSONObject(i).getString("comments")));
                        jdEliteVos.add(jdEliteGoodVo);
                }
                Map<String, Object> map = new HashMap<>(1);
                map.put("result", jdEliteVos);
                return ResultMap.ok(map);
        }
}
